package com.android.lyricshow.adapter;

import java.util.ArrayList;

import com.android.lyricshow.R;
import com.android.lyricshow.bean.AlbumBean;
import com.android.lyricshow.ui.ImageWorker;
import com.android.lyricshow.until.AppUntil;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AlbumAdapter extends BaseAdapter {

	private Context mContext;
	private ArrayList<AlbumBean> beanlist;
	
	private ImageWorker mImageFetcher;
	
	public AlbumAdapter(Context context, ImageWorker worker, ArrayList<AlbumBean> list){
		beanlist	= list;
		mContext	= context;
		
		mImageFetcher = worker;
	}
	
	@Override
	public int getCount() {
		return beanlist.size();
	}

	@Override
	public Object getItem(int position) {
		return beanlist.get(position);
	}

	@Override
	public long getItemId(int position) {
		return beanlist.get(position).getID();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder vh = null;
		
        if (convertView == null) {
        	convertView = LayoutInflater.from(mContext).inflate(R.layout.album_list_item, null);
			
			vh 			= new ViewHolder();
			
			vh.tvArtist = (TextView)convertView.findViewById(R.id.album_artist);
			vh.tvAlbum	= (TextView)convertView.findViewById(R.id.album_title);
			vh.iv 		= (ImageView)convertView.findViewById(R.id.album_view);
			
			convertView.setTag(vh);			
        } else {
        	vh = (ViewHolder)convertView.getTag();
        }
        
        AlbumBean abean = beanlist.get(position);
        
        vh.tvAlbum.setText(abean.getName()); 
        
        if(mImageFetcher != null){
        	vh.tvArtist.setText(beanlist.get(position).getArtist());
        	
        	mImageFetcher.loadImage(position, abean.getID(), vh.iv);
        }else{
        	vh.tvArtist.setVisibility(View.GONE);
        	
        	vh.iv.setImageBitmap(AppUntil.getBitmapFromDB(mContext, abean.getID(), AppUntil.SIZE_SMALL, R.drawable.noalbum));
        }
        
        return convertView;
	}

	static class ViewHolder{
		
		ImageView iv;
		TextView tvAlbum;
		TextView tvArtist;
	}

}
